/**
 * Project Name:MVC
 * File Name:LoginController.java
 * Package Name:cn.com.tupperware.controller
 * Date:Nov 3, 20163:02:18 PM
 * Copyright (c) 2016, chenzhou1025@126.com All Rights Reserved.
 *
*/

package cn.com.tupperware.controller;

import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import cn.com.tupperware.dao.AccountMapper;
import cn.com.tupperware.entity.Account;
import cn.com.tupperware.entity.AccountExample;
import cn.com.tupperware.entity.AccountExample.Criteria;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * ClassName:LoginController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: Nov 3, 2016 3:02:18 PM <br/>
 * 
 * @author upw023
 * @version
 * @since JDK 1.6
 * @see
 */
@Controller
@SessionAttributes("currUser")
public class LoginController {
	@Autowired
	private AccountMapper accountMapper;

//	@RequestMapping(value = { "/_login.do" }, method = RequestMethod.POST, produces = "text/json;charset=UTF-8")
//	@ResponseBody
//	public ModelAndView login(@ModelAttribute Login login, ModelMap model) throws IOException {
//		// System.out.println(login.getPassword());
//		AccountExample ex = new AccountExample();
//		Criteria criteria = ex.createCriteria();
//
//		criteria.andUsernameEqualTo(login.getUserName());
//		criteria.andPasswordEqualTo(login.getPassword());
//
//		List<Account> ac = accountMapper.selectByExample(ex);
//		Iterator<Account> iter = ac.iterator();
//		JSONArray array = new JSONArray();
//		JSONObject json = new JSONObject();
//		if (iter.hasNext()) {
//			json.put("islogin", true);
//			model.addAttribute("currUser", login.getUserName());
//		} else {
//			json.put("islogin", false);
//		}
//		array.add(json);
//		
//		// System.out.println(array.toString());
//		return new ModelAndView(new RedirectView("home.jsp"));
//	}

	@RequestMapping(value = { "/_mytest.do" }, method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView test(ModelMap model) throws IOException {
		// System.out.println(login.getPassword());

		model.addAttribute("currUser", "13");
		// System.out.println(array.toString());
		return new ModelAndView("test");
	}

	@RequestMapping(value = { "/_mytest2.do" }, method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView test2(HttpServletRequest request, ModelMap model) throws IOException {
		ModelAndView mav = new ModelAndView("test2");
		mav.addObject("time", new Date());
		mav.getModel().put("name", "caoyc");
		System.out.println(request.getSession().getAttribute("currUser"));
		return mav;
	}

	@RequestMapping(value = { "/_logout.do" }, method = RequestMethod.GET)
	public String logout(HttpServletResponse response,ModelMap
			 model, HttpSession session) {
		session.removeAttribute("currUser");

		model.remove("currUser");
		model.clear();
		session.invalidate();
		return "../../index";
	}

	// 登陆成功代码片段：
	// 1
	// session.setAttribute(GlobalConstants.SESSION_LOGIN_USER_NAME, user);
	//
	// 退出登录代码片段：
	// 1
	// User user = (User)
	// session.getAttribute(GlobalConstants.SESSION_LOGIN_USER_NAME);
	// 2
	// session.removeAttribute(GlobalConstants.SESSION_LOGIN_USER_NAME);
	// 3
	// response.sendRedirect("/");
	//
	// 如果有cookie的操作，则需要登录的时候写入cookie，代码如下：
	// 1
	// Cookie cookie = new
	// Cookie(GlobalConstants.COOKIE_NAME_ZUIDAIMA_ID,Security.encryptUserId(userId));
	// 2
	// cookie.setPath("/");
	// 3
	// cookie.setDomain(".zuidaima.com");
	// 4
	// cookie.setMaxAge(60 * 60 * 24 * 365 * 10);
	//
	// 退出的时候需要删除该cookie
	// 1
	// Cookie cookie = new
	// Cookie(GlobalConstants.COOKIE_NAME_ZUIDAIMA_ID,Security.encryptUserId(userId));
	// 2
	// cookie.setPath("/");
	// 3
	// cookie.setDomain(".zuidaima.com");
	// 4
	// cookie.setMaxAge(0);
	//
	// 注意：删除客户端cookie只需要设置cookie的max age值为0，并且必须给客户端response时set cookie才会删除。
	 @RequestMapping(value = { "/_login.do" }, method = RequestMethod.POST,
	 produces = "text/json;charset=UTF-8")
	 @ResponseBody
	 public String collectionQuestion(@ModelAttribute Login login, ModelMap
	 model) throws IOException {
	 // System.out.println(login.getPassword());
	 AccountExample ex = new AccountExample();
	 Criteria criteria = ex.createCriteria();
	
	 criteria.andUsernameEqualTo(login.getUserName());
	 criteria.andPasswordEqualTo(login.getPassword());
	
	 List<Account> ac = accountMapper.selectByExample(ex);
	 Iterator<Account> iter = ac.iterator();
	 JSONArray array = new JSONArray();
	 JSONObject json = new JSONObject();
	 if (iter.hasNext()) {
	 json.put("islogin", true);
	 model.addAttribute("currUser", login.getUserName());
	 } else {
	 json.put("islogin", false);
	 }
	 array.add(json);
	 //model.addAttribute("currUser", json);
	 // System.out.println(array.toString());
	 return array.toString();
	 }

	@RequestMapping(value = { "/_home.do" }, method = RequestMethod.GET, produces = "text/json;charset=UTF-8")

	public ModelAndView home(@ModelAttribute("currentUser") String json) throws Exception {
		ModelAndView mav = new ModelAndView("home");
		System.out.println(json);
		return mav;

	}

}
